package com.cskaoyan.service.wx.impl;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.wx.WXRegisterUser;
import com.cskaoyan.mapper.MarketCouponMapper;
import com.cskaoyan.mapper.MarketCouponUserMapper;
import com.cskaoyan.mapper.MarketUserMapper;
import com.cskaoyan.service.wx.WXAuthService;
import com.cskaoyan.util.MD5Util;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author Bowen Deng
 * @since 2022/06/29 21:48
 */
@Component
public class WXAuthServiceImpl implements WXAuthService {

    @Autowired
    MarketUserMapper marketUserMapper;


    @Autowired
    HttpServletRequest request;

    @Autowired
    MarketCouponMapper marketCouponMapper;

    @Autowired
    MarketCouponUserMapper marketCouponUserMapper;

    /**
     * 根据前台传入的数据注册
     * 705:手机号已注册
     * 703:验证码错误
     * 704:用户名已注册
     *
     * @param registerUser
     * @return java.lang.Integer
     * @author Bowen Deng
     * @since 2022/06/29 9:49 下午
     */
    @Override
    public Integer register(WXRegisterUser registerUser) {
        String code = marketUserMapper.selectCodeByPhone(registerUser.getMobile());
        String userCode = registerUser.getCode();
        if (!userCode.equals(code)) {
            return 703;
        }
        MarketUserExample marketUserExample = new MarketUserExample();
        marketUserExample.createCriteria().andUsernameEqualTo(registerUser.getUsername());
        List<MarketUser> marketUsers = marketUserMapper.selectByExample(marketUserExample);
        if (!marketUsers.isEmpty()) {
            for (MarketUser marketUser : marketUsers) {
                if(marketUser.getStatus()!=2){
                    return 704;
                }
            }

        }
        MarketUserExample phone = new MarketUserExample();
        phone.createCriteria().andMobileEqualTo(registerUser.getMobile());
        List<MarketUser> marketUsers1 = marketUserMapper.selectByExample(phone);
        if (!marketUsers1.isEmpty()) {
            for (MarketUser marketUser : marketUsers1) {
                if(marketUser.getStatus()!=2){
                    return 705;
                }
            }
        }
        MarketUser marketUser = new MarketUser();
        marketUser.setMobile(registerUser.getMobile());
        marketUser.setPassword(MD5Util.MD5EncodeUtf8(registerUser.getPassword()));
        marketUser.setDeleted(false);
        marketUser.setWeixinOpenid(registerUser.getWxCode());
        marketUser.setUsername(registerUser.getUsername());
        marketUser.setNickname(registerUser.getUsername());
        marketUser.setGender((byte) 0);
        marketUser.setBirthday(new Date());
        marketUser.setLastLoginTime(new Date());
        marketUser.setUserLevel((byte) 0);
        marketUser.setAvatar("https://yanxuan.nosdn.127.net/80841d741d7fa3073e0ae27bf487339f.jpg?imageView&quality=90&thumbnail=64x64");
        marketUser.setStatus((byte) 0);
        marketUser.setUpdateTime(new Date());
        marketUser.setAddTime(new Date());
        marketUser.setLastLoginIp(request.getRemoteAddr());
        marketUser.setSessionKey(SecurityUtils.getSubject().getSession().getId().toString());
        marketUserMapper.insert(marketUser);
        //根据type=1，status=0，deleted=false去查找所有的新人优惠券
        MarketCouponExample marketCouponExample = new MarketCouponExample();
        marketCouponExample.createCriteria().andTypeEqualTo((short) 1).andStatusEqualTo((short) 0).andDeletedEqualTo(false);
        List<MarketCoupon> marketCoupons = marketCouponMapper.selectByExample(marketCouponExample);
        if (!marketCoupons.isEmpty()) {
            for (MarketCoupon marketCoupon : marketCoupons) {
                if (marketCoupon.getTimeType() == 0) {
                    MarketCouponUser marketCouponUser = new MarketCouponUser();
                    marketCouponUser.setCouponId(marketCoupon.getId());
                    marketCouponUser.setUserId(marketUser.getId());
                    marketCouponUser.setAddTime(new Date());
                    marketCouponUser.setUpdateTime(new Date());
                    marketCouponUser.setStartTime(new Date());
                    marketCouponUser.setStatus((short) 0);
                    marketCouponUser.setEndTime(new Date(System.currentTimeMillis() + 10 * 24 * 60 * 60 * 1000));
                    marketCouponUser.setDeleted(false);
                    marketCouponUserMapper.insert(marketCouponUser);
                }
            }
        }
        marketUserMapper.deleteCodeByPhoneNumber(registerUser.getMobile());
        return 0;
    }

    @Override
    public MarketUser queryUserByName(String username) {
        MarketUserExample marketUserExample = new MarketUserExample();
        marketUserExample.createCriteria().andUsernameEqualTo(username);
        List<MarketUser> marketUsers = marketUserMapper.selectByExample(marketUserExample);
        return marketUsers.get(0);
    }

    /**
     * 703:验证码错误
     *
     * @param code
     * @param mobile
     * @param password
     * @return java.lang.Integer
     * @author Bowen Deng
     * @since 2022/06/30 9:11 上午
     */
    @Override
    public Integer reset(String code, String mobile, String password) {
        String rightCode = marketUserMapper.selectCodeByPhone(mobile);
        if (!code.equals(rightCode)) {
            return 703;
        }
        MarketUser marketUser = new MarketUser();
        marketUser.setPassword(password);
        MarketUserExample example = new MarketUserExample();
        example.createCriteria().andMobileEqualTo(mobile);
        marketUserMapper.updateByExampleSelective(marketUser, example);
        marketUserMapper.deleteCodeByPhoneNumber(mobile);
        return 0;
    }

    /**
     * 存储code
     *
     * @param phoneNumber
     * @param code
     * @return java.lang.Integer
     * @author Bowen Deng
     * @since 2022/06/30 9:38 上午
     */
    @Override
    public Integer insertCode(String phoneNumber, String code) {
        marketUserMapper.deleteCodeByPhoneNumber(phoneNumber);
        marketUserMapper.insertCode(phoneNumber, code);
        return null;
    }

    @Override
    public Integer isAvailable(String username) {
        MarketUserExample marketUserExample = new MarketUserExample();
        marketUserExample.createCriteria().andUsernameEqualTo(username);
        List<MarketUser> marketUsers = marketUserMapper.selectByExample(marketUserExample);
        if(marketUsers.isEmpty()){
            return 404;
        }
        MarketUser marketUser = marketUsers.get(0);
        if (marketUser.getStatus()!=0){
            return 400;
        }
        return 0;
    }
}
